<div class="container-nm tools">
    <h2 class="text-center mb-4">工具列表</h2>
    <table id="urlsTable" class="table table-striped table-bordered table-hover">
        <thead class="thead-dark">
            <tr>
                <th>ID</th>
                <th>上级</th>
                <th>名称</th>
                <th>连接</th>
                <th>图标</th>
                <th>css类</th>
                <th>是否新窗口打开</th>
                <th>分类</th>
                <th>排序</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <?php
            global $wpdb;

            // 使用try-catch块进行错误处理
            try {
                $results = $wpdb->get_results("SELECT * FROM ningm_tools_list");

                if ($results) {
                    foreach ($results as $row) {
                        // 使用数组和join方法减少echo的使用
                        $rowData = [
                            $row->id,
                            $row->parent,
                            $row->name,
                            $row->url,
                            $row->icon,
                            $row->class,
                            $row->target,
                            $row->type,
                            $row->sort,
                            '<button class="btn btn-primary btn-sm mr-2">编辑</button><button class="btn btn-danger btn-sm">删除</button>'
                        ];
                        echo '<tr><td>' . join('</td><td>', $rowData) . '</td></tr>';
                    }
                } else {
                    echo "<tr><td colspan='10' class='text-center'>没有符合条件的数据。</td></tr>";
                }
            } catch (Exception $e) {
                // 捕获并处理数据库查询错误
                echo "<tr><td colspan='10' class='text-center'>数据库查询错误: " . $e->getMessage() . "</td></tr>";
            }
            ?>
        </tbody>
    </table>
</div>
